<?php

namespace app\admin\controller\card;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Receivecard extends Backend
{
    
    /**
     * ReceiveCard模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('ReceiveCard');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //展示数据月份的选择
        $month = input('month',date('Y-m'));
        $begin = $month.'-01';
        $start_time = strtotime($month);
        $end_time = strtotime("$begin +1 month");


        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
//            if(session('admin.type') == 0){ //为超级管理员
//                $receive_model = Db::table('uct_receive_card')->alias('receive');
//            }else{// 1 2为商家 广告商
//                $receive_model = Db::table('uct_receive_card')->alias('receive')->where(['card.admin_id' => session('admin.id')]);
//            }

            //显示的字段
            $field = ['user.id','user.nickname','user.avatar','card.title','receive.createtime','store.s_name'];

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            //应用id
            $mp_id = get_admin_applet_info()['id'];

            //公用的模型
            $total_model = Db::table('uct_receive_card')->alias('receive')
                ->join('uct_wechat_card card','card.card_id = receive.card_id and receive.createtime between '.$start_time.' and '.$end_time)
                ->join('uct_user user','user.mp_openid = receive.user_openid and user.mp_id = '.$mp_id)
                ->join('uct_hk_store store','store.id = card.store_id')
                ->where($where);

            $list_model = Db::table('uct_receive_card')->alias('receive')
                ->join('uct_wechat_card card','card.card_id = receive.card_id and receive.createtime between '.$start_time.' and '.$end_time)
                ->join('uct_user user','user.mp_openid = receive.user_openid and user.mp_id = '.$mp_id)
                ->join('uct_hk_store store','store.id = card.store_id')
                ->where($where)
                ->field($field)
                ->limit($offset, $limit);

            //条件
            $receive_where = ['card.admin_id' => session('admin.id')];

            if(session('admin.type') == 0){ //为超级管理员
                $total = $total_model->count();
                $list = $list_model->select();
            }else{//广告商 商家
                $total = $total_model->where($receive_where)->count();
                $list = $list_model->where($receive_where)->select();
            }
//            $total = Db::table('uct_receive_card')->alias('receive')
//                ->join('uct_wechat_card card','card.card_id = receive.card_id')
//                ->join('uct_user user','user.mp_openid = receive.user_openid')
//                ->where($where)
//                ->order($sort, $order)
//                ->count();
//
//            $list = Db::table('uct_receive_card')->alias('receive')
//                ->join('uct_wechat_card card','card.card_id = receive.card_id')
//                ->join('uct_user user','user.mp_openid = receive.user_openid')
//                ->where($where)
//                ->field($field)
//                ->order($sort, $order)
//                ->limit($offset, $limit)
//                ->select();
            if(session('admin.type') != 0) {//非管理员
                foreach ($list as $key => &$val) {
                    $len = strlen($val['nickname']);
                    $first_str = mb_substr($val['nickname'], 0, 1, 'utf-8');
                    $val['nickname'] = $first_str . str_repeat('*', 4);
                }
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        //时间列表固定死
        $date_list = get_date_list();
        $param = '&month='.$month;
        $this->view->assign('param',$param);
        $this->view->assign('month',$month);
        $this->view->assign('url',url());
        $this->view->assign('date_list',$date_list);
        $this->view->assign('start_time',$start_time);
        $this->view->assign('end_time',$end_time);
        return $this->view->fetch();
    }

}
